package com.wanlian.socketserver.controller;

import com.wanlian.json.JsonResult;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.netty.model.Address;
import com.wanlian.netty.model.ResultInfo;
import com.wanlian.netty.msg.Device.model.RouterVersionReq;
import com.wanlian.netty.msg.Device.model.VersionUpgrade;
import com.wanlian.socketserver.Device.worker.DeviceServerWorker;
import com.wanlian.util.StringUtil;
import com.wanlian.vo.DeviceUpGradeVo;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.Map;

@Controller
@RequestMapping("/server")
@EnableAutoConfiguration
public class ServerController {

    @RequestMapping(value = "/alive")
    @ResponseBody
    public String alive() {
        return "alive";
    }

    @RequestMapping(value = "/list/online")
    @ResponseBody
    public String queryOnlineTimeList() {
        Map<String, Address> map = DeviceServerWorker.sessionPool.getAllRouter();
        return new JsonResult().Serailze(map,map.size());
    }

    @RequestMapping("/test")
    public ModelAndView test() {
        return new ModelAndView(new RedirectView("http://www.baidu.com"));
    }


    @RequestMapping(value = "/route/force/upgrade")
    @ResponseBody
    public String routerForceUpgrade(@RequestBody String data) {
        DeviceUpGradeVo deviceUpGradeVo = JsonStringUtil.readValue(data, DeviceUpGradeVo.class);
        Map<String, Address> routers = DeviceServerWorker.sessionPool.getAllRouter();

        for (String key : routers.keySet()) {
            Address address = routers.get(key);
            if (StringUtil.hasValue(address.getVersion())) {
                VersionUpgrade versionUpgrade = new VersionUpgrade();
                versionUpgrade.setMd5(deviceUpGradeVo.getMd5());
                versionUpgrade.setUrl(deviceUpGradeVo.getDownUrl());
                versionUpgrade.setVersion(deviceUpGradeVo.getVersionCode());
                RouterVersionReq routerVersionReq = new RouterVersionReq();
                routerVersionReq.setVersionUpgrade(versionUpgrade);
                routerVersionReq.setTagClientId(key);
                ResultInfo resultInfo = DeviceServerWorker.sendDataByKey(key, routerVersionReq);
                System.out.println("send update to key:" + key + " result:" + JsonStringUtil.writeValueAsString(resultInfo));
            }
        }
        return "";
    }

    @RequestMapping(value = "/route/force/upgradeone")
    @ResponseBody
    public String routerForceUpgradeone(@RequestBody String data) {
        DeviceUpGradeVo deviceUpGradeVo = JsonStringUtil.readValue(data, DeviceUpGradeVo.class);
        System.out.println("getDownUrl:" + deviceUpGradeVo.getDownUrl());
        System.out.println("getVersionCode:" + deviceUpGradeVo.getVersionCode());
        VersionUpgrade versionUpgrade = new VersionUpgrade();
        versionUpgrade.setMd5(deviceUpGradeVo.getMd5());
        versionUpgrade.setUrl(deviceUpGradeVo.getDownUrl());
        versionUpgrade.setVersion(deviceUpGradeVo.getVersionCode());
        RouterVersionReq routerVersionReq = new RouterVersionReq();
        routerVersionReq.setVersionUpgrade(versionUpgrade);
        routerVersionReq.setTagClientId(deviceUpGradeVo.getDeviceKey());
        DeviceServerWorker.sendDataByKey(deviceUpGradeVo.getDeviceKey(), routerVersionReq);
        return "";
    }
}
